package com.xhu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhu.exceptions.SaleplanException;
import com.xhu.mapper.SaleplanMapper;
import com.xhu.pojo.Saleplan;
import com.xhu.service.ISaleplanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class SaleplanServiceImpl extends ServiceImpl<SaleplanMapper, Saleplan> implements ISaleplanService {

    @Autowired
    private SaleplanMapper mapper;

    @Override
    public IPage<Saleplan> selectSaleplanToPages(Integer currentPage, Integer size) {
        //查询所有，直接将所有信息发送到前端
        return this.page(new Page<>(currentPage, size));
    }

    @Override
    public void commitPlan(Saleplan saleplan) {
        //判空
        if (saleplan == null || !StringUtils.hasText(saleplan.getSaleplanId())) {
            throw new SaleplanException("输入为空");
        }

        //检验是否有该计划
        if (!isExists(saleplan)) {
            throw new SaleplanException("不存在或数目异常");
        }

        //对表进行更新，将状态改为提交
        UpdateWrapper<Saleplan> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("saleplan_id", saleplan.getSaleplanId());
        updateWrapper.set(true, "status_name", STATUS_COMMIT)
                .set(true, "current_name", saleplan.getCurrentName());
        mapper.update(saleplan, updateWrapper);
    }

    @Override
    public void addPlan(Saleplan saleplan) {
        //判空
        if (saleplan == null || !StringUtils.hasText(saleplan.getSaleplanId())) {
            throw new SaleplanException("输入为空");
        }

        //禁止用户主动设置
        saleplan.setIsDelete(0)
                .setUpdateTime(null);

        //检验是否有该计划
        if (isExists(saleplan)) {
            throw new SaleplanException("该计划已存在");
        }


        //更新数据库
        int res = mapper.insert(saleplan);
        if (res != 1) {
            throw new SaleplanException("数据库操作失败");
        }

    }


    @Override
    public void updatePlan(Saleplan saleplan) {
        //判空
        if (saleplan == null || !StringUtils.hasText(saleplan.getSaleplanId())) {
            throw new SaleplanException("输入为空");
        }

        //禁止用户主动设置
        saleplan.setIsDelete(0)
                .setUpdateTime(null);

        LambdaUpdateWrapper<Saleplan> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Saleplan::getSaleplanId, saleplan.getSaleplanId());

        mapper.update(saleplan, wrapper);
    }


    @Override
    public void deleteById(String id) {
        mapper.deleteById(id);
    }


    @Override
    public IPage<Saleplan> getByDateTime(Integer currentPage, Integer size, String stringTime) {
        LambdaQueryWrapper<Saleplan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(true, Saleplan::getPlanMonth, stringTime);


        return page(new Page<>(currentPage, size), wrapper);

    }

    /**
     * 检测数据库中是否已经存在
     *
     * @param saleplan 实体
     * @return 有一个->true 其他->false
     */
    private boolean isExists(Saleplan saleplan) {
        //验证是否存在该计划
        QueryWrapper<Saleplan> wrapper = new QueryWrapper<>();
        wrapper.eq(true, "saleplan_id", saleplan.getSaleplanId());
        Long counts = mapper.selectCount(wrapper);
        return counts == 1;
    }


}
